package com.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ProductDto;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//        2.得到前端的值
        String name = req.getParameter("name");
        String imgHref = req.getParameter("imgHref");
        String content = req.getParameter("content");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String cuisineId = req.getParameter("cuisineId");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String enable = req.getParameter("enable");
//        3.封装到model
        ProductModel model = ProductModel.builder()
                .imgHref(imgHref)
                .marketPrice(Tool.strToDecimal(marketPrice))
                .normalPrice(Tool.strToDecimal(normalPrice))
                .content(content)
                .cuisineId(Tool.strToInt(cuisineId))
                .enable(Tool.strToInt(enable))
                .name(name)
                .page(Tool.strToPage(page))
                .limit(Tool.strToLimit(limit))
                .build();
//        4.调用服务逻辑层
        ProductService productService = new ProductServiceImpl();
        List<ProductDto> result = productService.findAll(model);
        int count=productService.count(model);
//        5.响应
        resp.getWriter().println(JSONObject.toJSONString(ResultDto.getInstance(count,result)));

    }
}
